<?php
require_once '../app/core/services/BaseService.php';
require_once '../app/models/DeliveryInfoDao.php';
require_once '../app/models/UserDao.php';
require_once '../app/models/CustomerWishlistDao.php';

class UserInfoService extends BaseService {
	private $deliveryInfoDao;
	private $userDao;
	private $customerWishlistDao;
	
    public function __construct(){
		parent::__construct();
		
		$this->deliveryInfoDao = new DeliveryInfoDao($this->dbUtil);
		$this->userDao = new UserDao($this->dbUtil);
		$this->customerWishlistDao = new CustomerWishlistDao($this->dbUtil);
	}
	
	public function saveDelivery($param){
		return $this->deliveryInfoDao->saveDelivery($param);
	}
	
	public function updateDelivery($param){
		 $this->deliveryInfoDao->updateDelivery($param);
	}
	
	public function getDeliveryList($userId){
	    
		$deliveries = $this->deliveryInfoDao->getDeliveriesOfUser($userId);
		$deliveryArr = array();
		if($deliveries){
			$areaService = new AreaService();
			
		    foreach($deliveries as $delivery){
		    	$province = $areaService->getAreaById($delivery['address_1']);
		        $deliveryArr[$delivery['id']]['province'] = $province[0]['title'];
		        $city = $areaService->getAreaById($delivery['address_2']);
		        $deliveryArr[$delivery['id']]['city'] = $city[0]['title'];
		        $county = $areaService->getAreaById($delivery['address_3']);
		        $deliveryArr[$delivery['id']]['county'] = $county[0]['title'];
		    }
		}
		return array($deliveries,$deliveryArr);
	}
	
	public function getDeliveryById($deliveryId){
		return $this->deliveryInfoDao->getDeliveryById($deliveryId);
	}
	
	public function delDelivery($deliveryId){
		$this->deliveryInfoDao->delDelivery($deliveryId);
	}
	
	public function updateAddress($param){
		$this->userDao->updateAddress($param);
	}
		
    public function updateUserPoints($int_userid, $int_offset, $bool_add = true, $bool_isBuyer = true){
        $arr_user = $this->userDao->getCustomerById($int_userid);
        if( !$arr_user )
            return false;
        $str_where = " `id` = '{$int_userid}'";
        if( !$bool_add )
            $int_offset = $int_offset * -1;
        $arr_param = array();
        $int_points = 0;
        if( $bool_isBuyer )
            $int_points = $arr_param['points_buyer'] = $arr_user['points_buyer'] + $int_offset;
        else 
            $int_points = $arr_param['points_seller'] = $arr_user['points_seller'] + $int_offset;
        if ( $this->userDao->update($arr_param, $str_where) )
            return $int_points;
        return false;
    }
    
    /*
     * 保存用户wishlist信息
     */
    public function saveUserWishlistInfo($data){
    	$this->customerWishlistDao->insert($data);
    }
    
    /*
     * 查找用户wishlist信息
     */
    public function getUserWishlistInfoByUserId($int_page = 1){
    	$userId = AAService::getCustomer();
        $itemsPerPage = $GLOBALS["config"]['items_per_page'];//akimimi set is 20
    	return $this->customerWishlistDao->getUserWishlistInfoByUserId( ( ($int_page - 1) * $itemsPerPage ), $itemsPerPage, $userId );
    }
    
    /**
     * 得到用户wishlist总记录数
     */
    public function getWishlistAllNumber(){
        $userId = AAService::getCustomer();
        $result = $this->customerWishlistDao->getAllNumber($userId);
        return $result[0]["allNum"];
    }
    
    /**
     * 删除用户wishlist记录
     */
    public function delWishlistItem($id){
        $userId = AAService::getCustomer();
        $data = array("is_delete" => 1);
        $where = "id = $id";
        $this->customerWishlistDao->update($data, $where);
    }
    
    /**
     * 更新用户wishlist的comment记录
     */
    public function updateWishlistComment($id, $comment){
        $userId = AAService::getCustomer();
        $data = array("comment" => $comment);
        $where = "id = $id";
        $this->customerWishlistDao->update($data, $where);
    }
	
}
?>